'use strict';
/**
 * @name 技术方案申请的controller
 * @author yud
 */

angular.module('metalabPartnerApp')
    .controller('TechnologyApplyCtrl', ['$scope', '$state', 'MetaForm', 'MetaResources', 'MetaRemind', 'MetaConstants', '$sce'
        , function ($scope, $state, MetaForm, MetaResources, MetaRemind, MetaConstants, $sce) {
            //variables
            $scope.clicked = [];
            $scope.stepValid = [];
            $scope.technologyFields = MetaConstants.technologyFields;

            $scope.formData = {
                'effectTags': [],
                'numberOfApplying': undefined,
                'numberOfApplied': undefined,
                'attachments': [],
                'title': undefined,
                'origin': '中国',
                'areas': [],
                'cooperationWays': [],
                'cooperationPrice': [],
                'technologyForm': undefined,
                'patentState': undefined,
                'maturity': undefined,
                'details': undefined,
                'advantages': undefined,
                'indicators': undefined,
                'applicableObjects': undefined,
                'detailsOfCases': undefined,
                'video': undefined,
                'contactInfo': {
                    'name': $scope.USER.name,
                    'phone': $scope.USER.mobile
                },
                'relationWithOwner': undefined,
                'ownedBy': undefined,
                'organization': undefined,
                'postOrTitle': undefined,
                'state': '新提交'
            };

            MetaResources.technology.origin(null, function (data) {
                $scope.origins = data;
            }, function (error) {
                MetaRemind.error(error);
            });

            //$scope.$watch('formData.video', function () {
            //    console.log($scope.formData.video);
            //    if ($scope.formData.video) {
            //        $scope.video.sources = [
            //            {
            //                src: $scope.formData.video.link,
            //                type: $scope.formData.video.type
            //            }
            //        ]
            //    }
            //}, true);

            //$scope.video = {
            //    sources: [{
            //        src: ('http://7xixj1.com1.z0.glb.clouddn.com/o_1aeouj31313uu12btr5r12ul1fj4e.mp4'),
            //        type: 'video/mp4'
            //    }],
            //    //    {
            //    //        src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/videos/videogular.webm"),
            //    //        type: "video/webm"
            //    //    },
            //    //    {
            //    //        src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/videos/videogular.ogg"),
            //    //        type: "video/ogg"
            //    //    }
            //    //],
            //    theme: "bower_components/videogular-themes-default/videogular.css",
            //    plugins: {
            //        poster: "http://www.videogular.com/assets/images/videogular.png"
            //    },
            //    tracks: [
            //        {
            //            src: "http://www.videogular.com/assets/subs/pale-blue-dot.vtt",
            //            kind: "subtitles",
            //            srclang: "en",
            //            label: "English",
            //            default: ""
            //        }
            //    ],
            //};


            $scope.submit = function () {

                var states = ['one', 'two', 'three'];
                var remind = ['一', '二', '三'];

                for (var i = 0; i < $scope.stepValid.length; ++i) {
                    if (!$scope.stepValid[i]) {
                        $state.go('^.' + states[i]);
                        MetaRemind.info('请完成步骤' + remind[i] + '的填写!');
                        return;
                    }
                }

                MetaResources.technology.create({userId: '_self'}, $scope.formData, function (data) {
                    MetaRemind.success('增加成功!');
                    $state.go('^.success', {
                        toState: 'meta.technology.list.released',
                        moduleName: 'technology',
                        moduleId: data.id
                    });
                }, function (error) {
                    MetaRemind.error(error);
                })
            };

            $scope.stepOneNext = function (valid) {
                $scope.clicked[0] = true;
                if (valid && $scope.formData.areas.length !== 0 && $scope.formData.cooperationWays.length !== 0) {
                    $scope.stepValid[0] = true;
                    $state.go('^.two');
                } else {
                    MetaRemind.error('请完整填写技术方案概要信息');
                }
            };

            $scope.stepTwoNext = function (valid) {
                $scope.clicked[1] = true;
                if (valid) {
                    $scope.stepValid[1] = true;
                    $state.go('^.three');
                } else {
                    MetaRemind.error('请完整填写技术方案详细信息');
                }
            };

            $scope.stepThreeNext = function (valid) {
                $scope.clicked[2] = true;
                if (valid) {
                    $scope.stepValid[2] = true;
                    $scope.submit();
                } else {
                    MetaRemind.error('请完整填写技术方案相关者信息');
                }
            };

        }]);
